package com.lagou.goods.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lagou.common.pojo.Goods;
import com.lagou.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/query/{id}")
    public Goods queryById(@PathVariable Integer id){
        System.out.println(goodsService.findById(id));
        return goodsService.findById(id);
    }

    @RequestMapping("/page/{yeshu}")
    public Map selectPage(@PathVariable Integer yeshu){
        Map map = new HashMap<>();
        IPage<Goods> iPage = goodsService.selectPage(yeshu);
        List<Goods> goodsList = iPage.getRecords();
        //存储 list:商品集合、total:总条目数
        map.put("list",goodsList);
        map.put("total",iPage.getTotal());
        return map;
    }

    @RequestMapping("/delete/{id}")
    public int goodsDeleteById(@PathVariable Integer id){
        int byId = goodsService.deleteById(id);
        return byId;
    }

    @RequestMapping("/update")
    public int goodsUpdateById(@RequestBody Goods goods){
        int byId = goodsService.updateById(goods);
        return byId;
    }

}
